package com.ohuang.launcher.tiktok;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.ohuang.launcher.hook.IsubHook;
import com.ohuang.launcher.hook.TelephonyManagerHook;
import com.ohuang.launcher.smallwindow.FloatViewManager;
import com.ohuang.launcher.smallwindow.WindowInterface;
import com.ohuang.launcher.smallwindow.WindowUI;
import com.ohuang.launcher.util.SPUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 用于tiktok逆向
 */
public class TikTokHook {
    public static final String TAG = "TikTokHook";

    public static String countryIso="";

    public static void init() {
        TelephonyManagerHook.getInstance().hook(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Log.i(TAG, "invoke: " + method.getName());
                Object invoke = method.invoke(proxy, args);
                if (invoke instanceof String) {
                    if ("getNetworkCountryIsoForPhone".equals(method.getName())) {
                        invoke = "";
                    } else {
                        invoke = "";
                    }
                } else if (invoke instanceof Integer) {
                    invoke = 1;
                }
                return invoke;
            }
        });
        IsubHook.getInstance().hook(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Log.i(TAG, "invoke: " + method.getName());
                Object invoke = method.invoke(proxy, args);
                if (method.getName().equals("getPhoneId")) {
                    invoke = -1;
                }
                return invoke;
            }
        });
    }

    public static String getCountryIso() {
        return countryIso;
    }

    public static void init(Application application) {
        countryIso = (String) SPUtil.getInstance("tiktokHook").get(application, "iso", "jp");
        FloatViewManager.getInstance().text="切换地区";
        FloatViewManager.getInstance().setWindowUiFactory(new FloatViewManager.WindowUiFactory() {
            @Override
            public WindowInterface create(Activity activity) {
                return new CoutryIsoWindowUI(activity);
            }
        });
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                FloatViewManager.getInstance().attach(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                FloatViewManager.getInstance().show(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
                FloatViewManager.getInstance().hide(activity);
            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                FloatViewManager.getInstance().onDestroy(activity);
            }
        });

    }
}
